package com.gruopbuying.agent.controller;

import com.gruopbuying.agent.connector.StoreFeign;
import com.gruopbuying.agent.constants.SysAgentInfoEnumConstants;
import com.gruopbuying.agent.context.FilterContextHandler;
import com.gruopbuying.agent.domain.AgentAuditStoreDO;
import com.gruopbuying.agent.dto.do2dto.AgentConvert;
import com.gruopbuying.agent.dto.AgentDTO;
import com.gruopbuying.agent.domain.SysAgentInfoDO;
import com.gruopbuying.agent.service.AgentAuditStoreService;
import com.gruopbuying.agent.service.SysAgentInfoService;
import com.gruopbuying.agent.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代理商信息表
 * 
 * @author WANGNIGN
 * @email wyn20140226@aliyun.com
 * @date 2019-07-22 17:48:37
 */
 
@RestController
@RequestMapping("/agent")
public class SysAgentInfoController {
	@Autowired
    private SysAgentInfoService sysAgentInfoService;

    @Autowired
    private AgentAuditStoreService agentAuditStoreService;

	private static final Logger logger = LoggerFactory.getLogger(SysAgentInfoController.class);
	@GetMapping()
	@RequiresPermissions("admin:sysAgentInfo:sysAgentInfo")
	String SysAgentInfo(){
	    return "admin/sysAgentInfo/sysAgentInfo";
	}
	
	@GetMapping("/list")
	@RequiresPermissions("admin:sysAgentInfo:sysAgentInfo")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        String agentId = FilterContextHandler.getAgentId();
		if(StringUtils.isEmpty(agentId)){
			agentId = "00000";
		}
		params.put("agentId",agentId);
		Query query = new Query(params);
		List<SysAgentInfoDO> sysAgentInfoList = sysAgentInfoService.list(query);
		int total = sysAgentInfoService.count(query);
		PageUtils pageUtils = new PageUtils(sysAgentInfoList, total);
		return pageUtils;
	}




	


	/**
	 * 获取代理商流水信息
	 */
	@GetMapping("/agentFlowList")
	public PageUtils agentFlowList(@RequestParam Map<String, Object> params){
		/*从token获取代理商id*/
		String agentId = FilterContextHandler.getAgentId();
		logger.info("SysAgentInfoController.agentFlowList()----->params:"+params.toString());
		logger.info("SysAgentInfoController.agentFlowList()----->获取到的代理商id为:"+agentId);
		params.put("agentId", agentId);
		//查询列表数据
		//--Query query = new Query(params);
        PageUtils sysAgentInfoList = sysAgentInfoService.agentFlowList(params);
		logger.info("SysAgentInfoController.agentFlowList()----->获取到的代理商sysAgentInfoList.total:"+sysAgentInfoList.getTotal());
		logger.info("SysAgentInfoController.agentFlowList()----->获取到的代理商sysAgentInfoList.rows:"+sysAgentInfoList.getRows());
		return sysAgentInfoList;
	}

	/**
	 * 通过代理商id获取服务商列表信息
	 */
	@GetMapping("/getStoreAccountList")
	public PageUtils getStoreAccountList(@RequestParam Map<String, Object> params){

		return sysAgentInfoService.getStoreAccountList(params);
	}
	/**
	 * 通过代理商id获取服务商门店列表信息
	 */
	@GetMapping("/storeMainInfoListByAgent")
	public PageUtils storeMainInfoListByAgent(@RequestParam Map<String, Object> params){

		return sysAgentInfoService.storeMainInfoListByAgent(params);
	}
	/**
	 * 通过代理商id获取门店运营人员列表信息
	 */
	@GetMapping("/getStoreOperList")
	public PageUtils getStoreOperList(@RequestParam Map<String, Object> params){
		logger.info("SysAgentInfoController.getStoreOperList()----->获取到的代理商门店运营人员为:"+params.toString());
		return sysAgentInfoService.getStoreOperList(params);
	}

    /**
     * 查询代理商注册的服务商列表
     * @auth cuich
     * @date 2019年8月5日15:28:24
     */
    @GetMapping("/getStoreList")
    public PageUtils getStoreList(@RequestParam Map<String, Object> params){
        logger.info("SysAgentInfoController-->getStoreList() params:::"+JsonUtils.objectToJson(params));
        Query query = new Query(params);
        List<AgentAuditStoreDO> agentAuditStoreList = agentAuditStoreService.list(query);
        int total = agentAuditStoreService.count(query);
        PageUtils page = new PageUtils(agentAuditStoreList,total);
        return page;
    }

    /**
     * 通过代理商id查询代理商信息
     */
	@RequestMapping("/queryAgent")
	public String queryAgent( @RequestParam Map<String, Object> params){
		logger.info("SysAgentInfoController.queryAgent() ----->通过代理商id查询代理商信息"+params.toString());
		String id = (String)params.get("agentId");
		SysAgentInfoDO agentInfoDO=this.sysAgentInfoService.get(id);
		String agentInfoJson=JsonUtils.beanToJson(agentInfoDO,"yyyyMMdd");
		return agentInfoJson;

	}

	@GetMapping("/queryAgentByArea")
	public Object queryAgentByArea( @RequestParam Map<String, Object> params){
		logger.error("SysAgentInfoController.queryAgentByArea() ----->通过代理商id查询代理商信息{}",JsonUtils.objectToJson(params));
		params.put("status", SysAgentInfoEnumConstants.ENUM_STATUS_START);
		List<SysAgentInfoDO>  sysAgentInfoDOS=this.sysAgentInfoService.list(params);
		String agentInfoJson=JsonUtils.beanToJson(sysAgentInfoDOS,"yyyyMMdd");
		return agentInfoJson;
	}
	/**
	 * 获取状态正常的代理商列表
	 * @auth wgq
	 * @date 2019年9月25日15:28:24
	 */
	@GetMapping("/getStartStatusAgentList")
	public R getStartStatusAgentList(){
		Map map = new HashMap();
		map.put("status", SysAgentInfoEnumConstants.ENUM_STATUS_START);
		List<SysAgentInfoDO> sysAgentInfoList = sysAgentInfoService.list(map);
		List<AgentDTO> agentDTOList = new ArrayList<>(sysAgentInfoList.size());
		for (SysAgentInfoDO SysAgentInfoDO : sysAgentInfoList){
			agentDTOList.add(AgentConvert.MAPPER.do2dto(SysAgentInfoDO));
		}
		return R.data(agentDTOList);
	}
}
